package com.hnkywl.sts.dao.sfgl;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.sfgl.Sfk;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: cyh
 * Date: 13-4-19
 * Time: 上午10:40
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class KhskdDao extends SimpleHibernateTemplate<Sfk, Long> {
    /**
     * 查询付款审核
     *
     * @param sbgsMc
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<Sfk> findSklxList(Date  skshStart,Date skshEnd, String bm, String sbgsMc,  String gldwMc, String zt, String skr,int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Sfk> page = new Page<Sfk>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (bm != null && !bm.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" bm like ? ");
            param.add("%" + bm.trim() + "%");
        }
        if (skr != null && !skr.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" createUser.xm like ? ");
            param.add("%" + skr.trim() + "%");
        }
        if (gldwMc != null && !gldwMc.isEmpty() && !gldwMc.equals("")) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" gldw.qc like ? ");
            param.add("%" + gldwMc.trim() + "%");
        }
        if (zt != null && !zt.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" zt = ? ");
            param.add(zt);
        }
        if (skshStart != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" dateCreated >= ? ");
            param.add(sDaysAfterNowDate(skshStart));
        }
        if (skshEnd != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" dateCreated < ? ");
            param.add(jDaysAfterNowDate(skshEnd));
        }
        if (sbgsMc != null && !sbgsMc.equals("")) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" sbgs.qc like ? ");
            param.add("%" + sbgsMc.trim() + "%");
        }

        //==============记录数量===============
        hql = "select count(*) from Sfk where sfklx in (3,4) " +
                "  and (createUser.id is null and sbgs.id not in (select id from Nbgs)) ";
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Sfk where sfklx in (3,4) " +
                "  and (createUser.id is null and sbgs.id not in (select id from Nbgs)) ";
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        hql = hql + " order by sfkrq desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public Date jDaysAfterNowDate(Date date) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.set(java.util.Calendar.HOUR_OF_DAY, 23);
        rightNow.set(java.util.Calendar.MINUTE, 59);
        rightNow.set(java.util.Calendar.SECOND, 59);
        return rightNow.getTime();
    }

    //   当前日期加减n天后的日期，返回String   (yyyy-mm-dd)
    public Date sDaysAfterNowDate(Date date) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.set(java.util.Calendar.HOUR_OF_DAY, 0);
        rightNow.set(java.util.Calendar.MINUTE, 0);
        rightNow.set(java.util.Calendar.SECOND, 0);
        return rightNow.getTime();
    }



}
